#include "usart.h"
#include "board.h"

/***********************************************串口1***********************************************/

/**
 * @brief 串口1初始化
 * @note 数据位：8 停止位：1 校验位：0 波特率：96500
 */
void USART1_Init(void)
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    //NVIC_InitTypeDef NVIC_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_DMA2, ENABLE); //GPIOA时钟使能
    //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_DMA2, ENABLE); //DMA时钟使能
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);                      //USART1时钟使能

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);  //GPIOA9，USART1，TX
    //GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); //GPIOA10，USART1，RX

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_DeInit(USART1);
    USART_InitStructure.USART_BaudRate = 96500;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_Mode = USART_Mode_Tx ;//| USART_Mode_Rx;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(USART1, &USART_InitStructure);
    USART_Cmd(USART1, ENABLE);

    USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
    USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
    //USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);

    // NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    // NVIC_Init(&NVIC_InitStructure);

    // DMA_DeInit(DMA2_Stream5);
    // Bluetooth_Rx_DMA_InitStructure.DMA_Channel = DMA_Channel_4;
    // Bluetooth_Rx_DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (USART1->DR);
    // Bluetooth_Rx_DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)tmp_RecvBuff;
    // Bluetooth_Rx_DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    // Bluetooth_Rx_DMA_InitStructure.DMA_BufferSize = sizeof(tmp_RecvBuff);
    // Bluetooth_Rx_DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    // Bluetooth_Rx_DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    // Bluetooth_Rx_DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    // Bluetooth_Rx_DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    // Bluetooth_Rx_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    // Bluetooth_Rx_DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    // Bluetooth_Rx_DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    // Bluetooth_Rx_DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
    // Bluetooth_Rx_DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    // Bluetooth_Rx_DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    // DMA_Init(DMA2_Stream5, &Bluetooth_Rx_DMA_InitStructure);
    // DMA_Cmd(DMA2_Stream5, ENABLE);

    // DMA_DeInit(DMA2_Stream7);
    // Bluetooth_Tx_DMA_InitStructure.DMA_Channel = DMA_Channel_4;
    // Bluetooth_Tx_DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (USART1->DR);
    // Bluetooth_Tx_DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&bluetooth_SendStruct;
    // Bluetooth_Tx_DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
    // Bluetooth_Tx_DMA_InitStructure.DMA_BufferSize = sizeof(bluetooth_SendStruct);
    // Bluetooth_Tx_DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    // Bluetooth_Tx_DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    // Bluetooth_Tx_DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    // Bluetooth_Tx_DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    // Bluetooth_Tx_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    // Bluetooth_Tx_DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    // Bluetooth_Tx_DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    // Bluetooth_Tx_DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
    // Bluetooth_Tx_DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    // Bluetooth_Tx_DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    // DMA_Init(DMA2_Stream7, &Bluetooth_Tx_DMA_InitStructure);
}

// /**
//  * @brief 串口1DMA发送
//  */
// void USART1_DMA_Send(void)
// {
//     DMA_Cmd(DMA2_Stream7,DISABLE);
//     while(DMA_GetCmdStatus(DMA2_Stream7) != DISABLE);
//     DMA_DeInit(DMA2_Stream7);
//     DMA_Init(DMA2_Stream7, &Bluetooth_Tx_DMA_InitStructure);
//     //打开DMA,开始发送
//     DMA_Cmd(DMA2_Stream7, ENABLE);
// }


/**
 * printf等输出流函数通过fputc这个函数实现
 * 修改fputc的定义，使之输出在STM32的寄存器中
 */
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
    int handle;
};
FILE __stdout;

//定义——sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
    x = x;
}

//重映射fputc函数，此函数为多个基础函数的输出函数
int fputc(int ch,FILE *f)
{
    while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
    USART_SendData(USART1,(uint8_t)ch);
    return ch;
}
                                /*
|---------------------------|
|                           |
| pritnf等函数可用做串口1打印 |
|                           |
|---------------------------|
                                */

// /**
//  * @brief 串口1接收中断
//  * @note 使用DMA
//  */
// void USART1_IRQHandler(void)
// {
// //	float recvTime = getSysTimeUs();
// //  u8 tmp;
//     if (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
//     {
//         // tmp = USART1->SR;
//         // tmp = USART1->DR;
//         DMA_Cmd(DMA2_Stream5,DISABLE);
//         USART_ClearITPendingBit(USART1, USART_IT_IDLE);
// /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓接收数据处理↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
//         if (tmp_RecvBuff[0] == 0xA5)
//             memcpy((u8*)(&bluetooth_RecvStruct), tmp_RecvBuff, sizeof(bluetooth_RecvStruct));
// /*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑接收数据处理↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
//         while(DMA_GetCmdStatus(DMA2_Stream5) != DISABLE);
//         DMA_DeInit(DMA2_Stream5);
//         DMA_Init(DMA2_Stream5, &Bluetooth_Rx_DMA_InitStructure);
//         DMA_SetCurrDataCounter(DMA2_Stream5, sizeof(tmp_RecvBuff));
//         DMA_Cmd(DMA2_Stream5,ENABLE);
//     }
// }

/***********************************************串口3***********************************************/

struct bluetooth_Recv_Struct_t bluetooth_RecvStruct;
struct bluetooth_Send_Struct_t bluetooth_SendStruct;

DMA_InitTypeDef Bluetooth_Rx_DMA_InitStructure;
DMA_InitTypeDef Bluetooth_Tx_DMA_InitStructure;

u8 tmp_RecvBuff[sizeof(bluetooth_RecvStruct)];


/**
 * @brief 串口3初始化
 */
void USART3_Init(void)
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA2, ENABLE); //GPIOA，DMA时钟使能
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);                      //USART1时钟使能

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_USART3);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART3);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    USART_DeInit(USART3);
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_Mode = USART_Mode_Rx;//|USART_Mode_Tx;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(USART3, &USART_InitStructure);
    USART_Cmd(USART3, ENABLE);

    USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
    USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);
//   USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    DMA_DeInit(DMA1_Stream1);
    Bluetooth_Rx_DMA_InitStructure.DMA_Channel = DMA_Channel_1;
    Bluetooth_Rx_DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (USART3->DR);
    Bluetooth_Rx_DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)tmp_RecvBuff;
    Bluetooth_Rx_DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    Bluetooth_Rx_DMA_InitStructure.DMA_BufferSize = sizeof(tmp_RecvBuff);
    Bluetooth_Rx_DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    Bluetooth_Rx_DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    Bluetooth_Rx_DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    Bluetooth_Rx_DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    Bluetooth_Rx_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    Bluetooth_Rx_DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    Bluetooth_Rx_DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    Bluetooth_Rx_DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
    Bluetooth_Rx_DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    Bluetooth_Rx_DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_Init(DMA1_Stream1, &Bluetooth_Rx_DMA_InitStructure);
    DMA_Cmd(DMA1_Stream1, ENABLE);

    // DMA_DeInit(DMA1_Stream3);
    // Bluetooth_Tx_DMA_InitStructure.DMA_Channel = DMA_Channel_3;
    // Bluetooth_Tx_DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (USART1->DR);
    // Bluetooth_Tx_DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&bluetooth_SendStruct;
    // Bluetooth_Tx_DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
    // Bluetooth_Tx_DMA_InitStructure.DMA_BufferSize = sizeof(bluetooth_SendStruct);
    // Bluetooth_Tx_DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    // Bluetooth_Tx_DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    // Bluetooth_Tx_DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    // Bluetooth_Tx_DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    // Bluetooth_Tx_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    // Bluetooth_Tx_DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    // Bluetooth_Tx_DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    // Bluetooth_Tx_DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
    // Bluetooth_Tx_DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    // Bluetooth_Tx_DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    // DMA_Init(DMA1_Stream3, &Bluetooth_Tx_DMA_InitStructure);
}

// /**
//  * @brief 串口3DMA发送
//  */
// void USART3_DMA_Send(void)
// {
//     DMA_Cmd(DMA1_Stream3,DISABLE);
//     while(DMA_GetCmdStatus(DMA1_Stream3) != DISABLE);
//     DMA_DeInit(DMA1_Stream3);
//     DMA_Init(DMA1_Stream3, &Bluetooth_Tx_DMA_InitStructure);
//     //打开DMA,开始发送
//     DMA_Cmd(DMA1_Stream3, ENABLE);
// }

/**
 * @brief 串口3接收中断
 * @note 使用DMA
 */
void USART3_IRQHandler(void)
{
//	float recvTime = getSysTimeUs();
//  u8 tmp;
    if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)
    {
        // tmp = USART1->SR;
        // tmp = USART1->DR;
        DMA_Cmd(DMA1_Stream1,DISABLE);
        USART_ClearITPendingBit(USART3, USART_IT_IDLE);
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓接收数据处理↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
        if (tmp_RecvBuff[0] == 0xA5)
            memcpy((u8*)(&bluetooth_RecvStruct), tmp_RecvBuff, sizeof(bluetooth_RecvStruct));
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑接收数据处理↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
        while(DMA_GetCmdStatus(DMA1_Stream1) != DISABLE);
        DMA_DeInit(DMA1_Stream1);
        DMA_Init(DMA1_Stream1, &Bluetooth_Rx_DMA_InitStructure);
        DMA_SetCurrDataCounter(DMA1_Stream1, sizeof(tmp_RecvBuff));
        DMA_Cmd(DMA1_Stream1,ENABLE);
    }
}


